package com.future.mayi.activity;


import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.future.mayi.R;
import com.future.mayi.utils.HtmlFormat;
import com.future.mayi.utils.MyWebViewClient;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 周大虎 on 2017/11/7.
 */

public class WebActivity extends BaseWebViewActivity {

    String url;
    String title;
    String content;
    @BindView(R.id.progress_bar)
    ProgressBar progressBar;
    @BindView(R.id.web_view)
    WebView webView;
    @BindView(R.id.iv_title_close)
    ImageView ivTitleClose;
    @BindView(R.id.tv_title)
    TextView tvTitle;


    public static void startActivity(Context context, String url, String title) {
        Intent intent = new Intent(context, WebActivity.class);
        intent.putExtra("url", url);
        if (title != null)
            intent.putExtra("title", title);
        context.startActivity(intent);
    }

    public static void startActivity(String content, String title, Context context) {
        Intent intent = new Intent(context, WebActivity.class);
        intent.putExtra("content", content);
        if (title != null)
            intent.putExtra("title", title);
        context.startActivity(intent);
    }


    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_web);
        ButterKnife.bind(this);

        if (getIntent() != null) {
            url = getIntent().getStringExtra("url");
            title = getIntent().getStringExtra("title");
            content = getIntent().getStringExtra("content");
        }

        tvTitle.setText(TextUtils.isEmpty(title) ? "" : title);


        if (TextUtils.isEmpty(url) && TextUtils.isEmpty(content))
            finish();
    }

    @Override
    protected void initView() {
        webView.setWebViewClient(new MyWebViewClient(progressBar, this));
        initWebView(webView, null, progressBar);


    }

    @Override
    protected void initDates() {
        if (!TextUtils.isEmpty(url))
            loadUrl(url);


        if (!TextUtils.isEmpty(content))
            webView.loadDataWithBaseURL(null, HtmlFormat.getNewContent(content), "text/html", "utf-8", null);
    }

    @Override
    protected void initListener() {

    }


    @Override
    protected void onLoadTitle(String title) {
        tvTitle.setText(TextUtils.isEmpty(title) ? "" : title);
    }


    @OnClick({R.id.iv_title_left, R.id.iv_title_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                onBackPressed();
                break;
            case R.id.iv_title_close:
                finish();
                break;
        }
    }
}
